package cn.codergege.kotlinclass.classandinheritance

open class BaseA

class DerivedA: BaseA()


open class BaseB(val a: Int, val b: Int?, val c: Int) {
    constructor(a: Int, c: Int): this(a, null, c)
}

//  用次构造函数也是可以的
class DerivedB(a: Int, b: Int?, c: Int, val d: Int, val e: Int): BaseB(a, c)
/*
会生成这样的构造函数:
   public DerivedB(int a, @Nullable Integer b, int c, int d, int e) {
      super(a, c);
      this.d = d;
      this.e = e;
   }
 */

// 如果派生类没有主构造函数
class DerivedBB: BaseB {
    // 因为属性必须初始化, 如果没有直接赋值, 就得必须写进构造函数中
    var d: Int
    var e: Int
    // 使用次构造函数继承父类, 得用 super 关键字
    constructor(a: Int, b: Int?, c:Int, d: Int, e:Int): super(a, b, c) {
        this.d = d
        this.e = e
    }
    constructor(a: Int, c: Int, d: Int, e: Int): super(a, c) {
        this.d = d
        this.e = e
    }
}

// 重写方法
open class BaseC {
    open fun funC() {
        println("funC")
    }
}

class DerivedC: BaseC() {
    override fun funC() {
//        super.funC()
        println("重写后的 funC")
    }
}

// 重写属性
open class BaseD {
    open var d: Int = 0
    // 这个是没有 backing field 的
    open val dd get() = true
}

class DerivedD: BaseD() {
    override var d: Int = 1
    override var dd = false
}

open class BaseE {
    open fun funE() { println("BaseE.funE()") }
    open val e get() = 1
}

class DerivedE: BaseE() {
    override fun funE() {
        super.funE()
        println("DerivedE.funE()")
    }
    override val e get() = super.e + 1
    inner class InnerE {
        fun f() {
            super@DerivedE.funE()
            println(super@DerivedE.e)
        }
    }
}



open class BaseF {
    open fun f() {
        println("BaseF.f()")
    }
    open val f get() = "BaseF.f"
}
interface IBaseG {
    fun f() {
        println("IBaseG.f()")
    }
    val f get() = "IBaseG.f"
}

class DerivedFG: BaseF(), IBaseG {
    override fun f() {
        super<BaseF>.f()
        super<IBaseG>.f()
    }
    override val f get() = super<BaseF>.f + super<IBaseG>.f
}

open class BaseH {
    open fun f() {}
}

abstract class DerivedH : BaseH() {
    override abstract fun f()
}

class MyClass {
    // 可以省略伴生类的类名
    companion object {
        fun create(): MyClass = MyClass()
    }
}
fun main(args: Array<String>) {
    val derivedC = DerivedC()
    derivedC.funC()
    // val baseD = BaseD(1)
    // println("baseD.d: ${baseD.d}")
    val instance = MyClass.create()
}